package com.liner.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.liner.entity.User;
import com.liner.enums.ResponseEnum;
import com.liner.exception.ShopException;
import com.liner.form.UserForm;
import com.liner.result.Result;
import com.liner.service.UserService;
import com.liner.utils.JwtUtil;
import com.liner.utils.MD5Util;
import com.liner.utils.RegexValidateUtil;
import com.liner.vo.UserVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author liner
 * @since 2022-12-04
 */
@RestController
@RequestMapping("/user")
@Api(tags = "用户接口", description = "用户登录接口")
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private RedisTemplate redisTemplate;

    private LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();

    @ApiOperation("注册用户")
    @PostMapping("/register")
    public Result register(@RequestBody UserForm userForm) {
        verify(userForm);

        //手机号是否已注册
        queryWrapper.eq(User::getMobile, userForm.getMobile());
        User one = userService.getOne(queryWrapper);
        if (one != null) {
            throw new ShopException(ResponseEnum.MOBILE_EXIST.getMsg());
        }

        //验证码校验
        String code = (String) redisTemplate.opsForValue().get("LinShop-sms-code-" + userForm.getMobile());
        System.out.println(code);

        if (!code.equals(userForm.getCode())) {
            throw new ShopException(ResponseEnum.SMS_CODE_ERROR.getMsg());
        }

        User user = new User();
        user.setMobile(userForm.getMobile());
        user.setPassword(MD5Util.getSaltMD5(userForm.getPassword()));
        userService.save(user);

        return Result.ok();
    }


    @ApiOperation("用户登录")
    @GetMapping("/login")
    public Result login(UserForm userForm){
        verify(userForm);

        //手机号是否已注册
        queryWrapper.eq(User::getMobile, userForm.getMobile());
        User one = userService.getOne(queryWrapper);
        if (one == null) {
            throw new ShopException(ResponseEnum.MOBILE_IS_NULL.getMsg());
        }

        //验证密码是否一致
        if (!MD5Util.getSaltverifyMD5(userForm.getPassword(),one.getPassword())) {
            throw new ShopException(ResponseEnum.PASSWORD_ERROR.getMsg());
        }

        //生成token
        String token = JwtUtil.createToken(one.getUserId(), one.getMobile());
        UserVo userVo = new UserVo(one.getUserId(),one.getPassword(), one.getMobile(),token);

        return Result.ok(userVo);
    }



    @ApiOperation("Token验证")
    @ApiImplicitParam(name = "token",value="token")
    @GetMapping("/checkToken")
    public Result getCheckToken(HttpServletRequest request){
        String token = request.getHeader("token");
        boolean result = JwtUtil.checkToken(token);
        if(result) return Result.ok();
        return Result.fail(ResponseEnum.TOKEN_ERROR.getMsg());
    }


    /**
     * 封装公共验证方法
     * @param userForm
     */
    private void verify(UserForm userForm) {
        //手机号格式是否正确
        boolean b = RegexValidateUtil.checkMobile(userForm.getMobile());
        if (!b) {
            throw new ShopException(ResponseEnum.MOBILE_ERROR.getMsg());
        }
    }

}

